var data_report = null;
var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}
document.getElementById("starTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";

function search() {
    var starTime = $("#starTime").val();
    var endTime = $("#endTime").val();
    var statusClass = $("#statusClass").val();
    var accordingTime = $("#accordingTime").val();
    $.ajax({
        type: "post",
        url: Feng.ctxPath + "/reservationReport/getReservationReport",
        data: {
            "starTime": starTime,
            "endTime": endTime,
            "accordingTime": accordingTime,
            "statusClass": statusClass
        },
        dataType: "json",
        success: function (data) {
            var arr = [];
            if ($("#statusClass").val() === '年龄') {
                data.forEach(function (v1) {
                    var obj = {};
                    obj['时间'] = v1['时间'];
                    obj['合计'] = v1['合计'];
                    obj['0-9'] = v1['0-9'];
                    obj['10-19'] = v1['10-19'];
                    obj['20-29'] = v1['20-29'];
                    obj['30-39'] = v1['30-39'];
                    obj['40-49'] = v1['40-49'];
                    obj['50-59'] = v1['50-59'];
                    obj['60-69'] = v1['60-69'];
                    obj['70-79'] = v1['70-79'];
                    obj['80-89'] = v1['80-89'];
                    obj['90-99'] = v1['90-99'];
                    arr.push(obj);
                });
            } else if ($("#statusClass").val() === '总计报表') {
                data.forEach(function (v1) {
                    var obj = {};
                    obj['时间'] = v1['时间'];
                    obj['合计'] = v1['合计'];
                    obj['预约'] = v1['预约'];
                    obj['过期'] = v1['过期'];
                    obj['待定'] = v1['待定'];
                    obj['到店'] = v1['到店'];
                    obj['到店比率'] = v1['到店比率'];
                    arr.push(obj);
                });
            }
            else {
                data.forEach(function (v1) {
                    var obj = {};
                    obj['时间'] = v1['时间'];
                    obj['合计'] = v1['合计'];
                    Object.keys(v1).forEach(function (v2) {
                        if (v2 !== '时间' && v2 !== '合计') {
                            obj[v2] = v1[v2]
                        }
                    });
                    arr.push(obj);
                });
            }
            data_report = arr;
            renders();
        },
        error: function (msg) {
            alert(msg);
        }
    });
}

function resetSearch() {
    document.getElementById("starTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    $("#statusClass").val('总计报表');
    $("#accordingTime").val('年');
}

function renders() {
    var report_table = $("#report_table")[0];
    var table_th = document.createElement("tr");
    var str_th = '';
    var ofragment = document.createDocumentFragment();
    $("#report_table").empty();
    table_th.class = 'table-bordered';
    Object.keys(data_report[0]).forEach(function (v) {
        str_th += "<th>" + v + "</th>";
    });
    table_th.innerHTML = str_th;
    report_table.appendChild(table_th);
    data_report.forEach(function (v1) {
        var str_td = '';
        var tr = document.createElement("tr");
        Object.keys(v1).forEach(function (v2) {
            str_td += "<td>" + v1[v2] + "</td>";
        });
        tr.innerHTML = str_td;
        ofragment.appendChild(tr);
    });
    report_table.appendChild(ofragment);
}